package com.party.core.service.member.util;

import com.party.common.utils.DateUtils;
import com.party.common.utils.SpringContextHolder;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.MemberGroupInfo;
import com.party.core.service.member.IMemberGroupInfoService;
import com.party.core.service.member.IMemberGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * 表单帮助类
 */
public class MemberFormUtil {
    static IMemberGroupInfoService memberGroupInfoService = SpringContextHolder.getBean(IMemberGroupInfoService.class);

    static IMemberGroupService memberGroupService = SpringContextHolder.getBean(IMemberGroupService.class);

    static Logger logger = LoggerFactory.getLogger(MemberFormUtil.class);

    /**
     * 根据机构id获取机构到期时间
     *
     * @param memberId
     * @return
     */
    public static Date getExpirationTimeById(String memberId) {
        memberId = memberGroupService.getPartnerId(memberId);
        MemberGroupInfo memberGroupInfo = memberGroupInfoService.findByMemberId(memberId);
        if (null == memberGroupInfo || null == memberGroupInfo.getExpirationTime()) {
            logger.error("{}的memberGroupInfo为空或者到期时间为空", memberId);
            return null;
        }
        return memberGroupInfo.getExpirationTime();
    }

    /**
     * @param date    日期
     * @param pattern 格式
     * @return java.lang.String
     * @Author yifeng
     * @Description 格式化
     * @Date 10:52 2019/2/25
     **/
    public static String dateFormat(Date date, String pattern) {
        if (null != date) {
            return DateUtils.formatDate(date, pattern);
        }
        return new String();
    }

    /**
     * @param memberId   机构id
     * @param expireDate 过期时间
     * @return boolean true为过期/false为没过期
     * @Author yifeng
     * @Description 检查机构是否过期
     * @Date 17:56 2019/2/27
     **/
    public static boolean isExpire(String memberId, Date expireDate) {
        if (null == expireDate) {
            expireDate = getExpirationTimeById(memberId);
        }
        if (null != expireDate) {
            if (new Date().before(expireDate)) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据id获取用户昵称
     * @param id
     * @return
     */
    public static String getRealNameById(String id) {
        MemberGroup member = memberGroupService.get(id);
        if (member != null) {
            return member.getRealname();
        }
        return "";
    }
}
